<template>
    <a-drawer :title="title" :width="400" @close="handleCancel" :visible="visible" :maskClosable="false"
              :bodyStyle="{ paddingBottom: '60px' }">
        <a-form-model ref="ruleForm" :model="form" :rules="rules">

            <a-form-model-item label="相册名称" prop="name" ref="name">
                <a-input placeholder="请设置相册名称" v-model="form.name"/>
            </a-form-model-item>

            <a-form-model-item label="所属酒店" prop="type" ref="type">
                <a-select placeholder="请选择相册所属酒店" allowClear showSearch v-model="form.hotelId"
                          @change="getRoomType()"
                >
                    <a-select-option v-for="(item, index) in HotelProfileOPT" :key="index" :value="item.value">
                        {{item.label }}
                    </a-select-option>
                </a-select>
            </a-form-model-item>


            <a-form-model-item label="相册封面图" prop="coverPhoto" ref="coverPhoto">
                <a-upload
                        list-type="picture-card"
                        class="avatar-uploader"
                        :show-upload-list="false"
                        :before-upload="beforeUpload"
                        @change="handleChange"
                        :customRequest="customRequest"
                >
                    <img v-if="form.coverPhoto" :src="form.coverPhoto" class="picture" alt="请上传图片"/>
                    <div v-else>
                        <a-icon :type="loading ? 'loading' : 'plus'"/>
                        <div class="ant-upload-text">请设置一张相册封面图</div>
                    </div>
                </a-upload>
            </a-form-model-item>


            <a-form-model-item label="相册类型" prop="type" ref="type">
                <a-select placeholder="请选择相册所属类型" allowClear showSearch v-model="form.type"  @change="getRoomType()">
                    <a-select-option v-for="(item, index) in AlbumTypeOPT" :key="index" :value="item.value">
                        {{item.label }}
                    </a-select-option>
                </a-select>
            </a-form-model-item>


            <a-form-model-item v-if="form.type===FXXCVAL"
                               label="关联房型"
                               prop="roomTypeId"
                               ref="roomTypeId">
                <a-select placeholder="请选择相册关联房型"
                          allowClear
                          showSearch
                          v-model="form.roomTypeId"
                          @change="getRoomType()"
                >
                    <a-select-option v-for="(item, z) in HotelRoomTypeOPT" :key="z" :value="item.value">
                        {{item.label }}
                    </a-select-option>
                </a-select>
            </a-form-model-item>


        </a-form-model>
        <!-- Modal 页脚 -->
        <footer class="drawer-footer">
            <a-button type="primary" :loading="loading" @click="onSubmit">保存</a-button>
        </footer>
    </a-drawer>
</template>
<script>
    import {hotelAlbumAdd, hotelAlbumUpdate} from '../public/http.js';

    import {dictLoad, hotelProfileDict, hotelRoomTypeDict, fileUpload} from '../../../util/internal.api';

    export default {
        props: ['editRecord'],


        data() {
            return {
                FXXCVAL: 'XC02', //房型相册对应的VALUE
                isUpdate: false, //是否是更新模式
                loading: false, //按钮Loading
                title: '创建相册',
                visible: true, //modal 显示状态
                AmapSectorShow: false,

                form: {
                    name: null,
                    hotelId: undefined,
                    coverPhoto: null,

                    type: undefined,
                    roomTypeId: undefined,
                },
                rules: {
                    coverPhoto: {
                        required: true,
                        message: '请上传一张酒店封面图',
                        trigger: 'blur'
                    },

                    name: [
                        {
                            required: true,
                            message: '酒店名称不能为空',
                            trigger: 'blur'
                        },
                        {
                            max: 20,
                            message: '酒店名称不能超过20个字',
                            trigger: 'blur'
                        }
                    ],


                    telNumber: [
                        {
                            required: true,
                            message: '酒店订房电话不能为空',
                            trigger: 'blur'
                        },
                        {
                            max: 50,
                            message: '酒店电话号码长度不超过50个字符',
                            trigger: 'blur'
                        }

                    ],

                    lng: {
                        required: true,
                        message: '请标注酒店位置',
                        trigger: 'blur'
                    },

                    address: {
                        required: true,
                        message: '请填写酒店详细地址',
                        trigger: 'blur'
                    },


                },
                AlbumTypeOPT: [],
                HotelProfileOPT: [],
                HotelRoomTypeOPT: [],

            };
        },
        methods: {
            //位置选择回调
            closeModalHandler(e) {

                this.AmapSectorShow = false;
                if (e.location) {
                    let {lng, lat, address} = e.location;
                    let ob = {lng, lat, address};
                    //合并对象
                    this.form = Object.assign(this.form, ob)
                }


            },

            //表单提交事件
            onSubmit() {
                this.$refs.ruleForm.validate(valid => {
                    //验证成功后进行数据提交
                    if (valid) {
                        this.loading = true;
                        if (this.isUpdate) {
                            hotelAlbumUpdate(this.form.id, this.form).then(res => {
                                this.$exMod.ajaxMessage(res);
                                if (res.code === 0) {
                                    this.handleCancel(true);
                                }
                            });

                        } else {
                            hotelAlbumAdd(this.form).then(res => {
                                this.$exMod.ajaxMessage(res);
                                if (res.code === 0) {
                                    this.handleCancel(true);
                                }
                            });
                        }
                    }
                });
            },

            //获取酒店房型
            getRoomType() {
                if (this.form.hotelId && this.form.type === this.FXXCVAL) {
                    this.HotelRoomTypeOPT = [];
                    hotelRoomTypeDict(this.form.hotelId).then(res => this.HotelRoomTypeOPT = res);
                }

            },


            //关闭弹窗
            handleCancel(render = false) {
                this.visible = false;
                this.$emit('closeModal', {render: render});
            },
            //表单初始化
            fromVal() {
                let obj = {...this.editRecord};
                if (obj.id) {

                    this.form = obj;
                    this.getRoomType();
                    this.isUpdate = true;
                    this.title = '编辑相册';
                }
            },
            //数据初始化result
            async init() {

                await dictLoad("HotelAlbumType").then(res => this.AlbumTypeOPT = res);
                await hotelProfileDict().then(res => this.HotelProfileOPT = res);

                this.fromVal();

            },


            //酒店系统长传方法
            customRequest(e) {
                const formData = new FormData();
                formData.append('file', e.file)
                fileUpload(formData).then((res) => {
                    this.loading = false;
                    if (res.code === 0) {
                        this.form.coverPhoto = res.data.url;
                    }
                })
            },
            //监听上传状态  uploading 开始
            handleChange(info) {

                if (info.file.status === 'uploading') {
                    this.loading = true;
                }
            },
            //上传之前数据检查
            beforeUpload(file) {
                const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
                if (!isJpgOrPng) {
                    this.$message.error('只允许上传JPG、PNG格式的图片文件');
                }
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                    this.$message.error('单个文件图片不能超过 2MB!');
                }
                return isJpgOrPng && isLt2M;
            },

        },

        created() {
            this.init();
        }
    };
</script>

<style scoped>
    .picture {
        width: 200px;
        height: 100px;
        overflow: hidden;
    }
</style>
